using QxFramework.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerManager : LogicModuleBase, IPlayerManager
{
    private PlayerControl player;

    public PlayerControl Player
    {
        get
        {
            if (player == null)
            {
                player = GameObject.FindObjectOfType<PlayerControl>();
            }
            return player;
        }
    }

    public float GetHealth()
    {
        return Player.health;
    }

    public float GetMaxHealth()
    {
        return Player.maxHealth;
    }

    public void HealthChange(float value)
    {
        if (Player.health + value > Player.maxHealth)
        {
            Player.health = Player.maxHealth;
        }
        else if (Player.health + value <= 0)
        {
            Player.health = 0;
            Die();
        }
        else
        {
            Player.health += value;
        }
    }

    public void MaxHealthChange(float value)
    {
        if (Player.maxHealth + value < 0)
        {
            Debug.LogError("Player's max health can't go below zero!");
        }
        else
        {
            Player.maxHealth += value;
        }
    }

    public void Damage(float damage, DamageArgs args = null)
    {
        HealthChange(-damage);
        if (args != null)
        {
            GameMgr.Get<IEffectManager>().InstantiateEffect("BloodEffect", args.source, args.directionAngle);
        }
        float r = Random.value;
        int n = (int)(r / (1f / 5f));
        if(n==0)
        {

        }
        else
        {
            AudioControl.Instance.PlaySound("hurt-" + n, 0.1f);
        }
    }

    public void Die()
    {
        GameMgr.Get<IEffectManager>().InstantiateEffect("BloodEffect", player.transform.position);
        GameMgr.Get<IEffectManager>().InstantiateEffect("BloodEffect", player.transform.position);
        GameMgr.Get<ICameraManager>().isLocked(false);
        UIManager.Instance.Open("Die", "", 5);
        Player.isDead = true;
        Debug.Log("YOU DIED");
    }
}

public class DamageArgs
{
    public Vector2 source;
    public float directionAngle;
    //others
}